package com.innovation.chengzhangriji.ui.widget;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;

public class DiscoverCommentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements DiscoverCommentViewHolder.OnClickDiscoverCommentListener, DiscoverCommentLoadHolder.OnClickLoadMoreListener {

    @Override
    public void loadMore() {
        if(mLoadMoreListener != null) {
            getLoadMoreListener().loadMore();
        }
    }

    private DiscoverCommentLoadHolder.OnClickLoadMoreListener mLoadMoreListener;
    private DiscoverCommentViewHolder.OnClickDiscoverCommentListener mCommentListener;

    public DiscoverCommentLoadHolder.OnClickLoadMoreListener getLoadMoreListener() {
        return mLoadMoreListener;
    }

    public void setLoadMoreListener(DiscoverCommentLoadHolder.OnClickLoadMoreListener loadMoreListener) {
        mLoadMoreListener = loadMoreListener;
    }

    public DiscoverCommentViewHolder.OnClickDiscoverCommentListener getCommentListener() {
        return mCommentListener;
    }

    public void setCommentListener(DiscoverCommentViewHolder.OnClickDiscoverCommentListener commentListener) {
        mCommentListener = commentListener;
    }

    public interface Observer {

        int length();

        int type(int position);

        String userName(int position);

        String userUrl(int position);

        String comment(int position);

        boolean like(int position);

        String time(int position);

        String from(int position);

        String imageUrl(int position);

        String duration(int position);

        String text(int position);
    }

    public static final int TEXT = 0;

    public static final int PHOTO = 1;

    public static final int AUDIO = 2;

    public static final int VIDEO = 3;

    public static final int MILESTONE = 4;

    public static final int LOAD_MORE = 15;

    private Observer observer;

    public boolean isEnd() {
        return end;
    }

    public void setIsEnd(boolean isEnd) {
        this.end = isEnd;
    }

    private boolean end = true;

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_discover_comment, parent, false);
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case TEXT:
                holder = new DiscoverCommentTextViewHolder(view);
                break;
            case PHOTO:
                holder = new DiscoverCommentPhotoViewHolder(view);
                break;
            case AUDIO:
                holder = new DiscoverCommentAudioViewHolder(view);
                break;
            case VIDEO:
                holder = new DiscoverCommentVideoViewHolder(view);
                break;
            case MILESTONE:
                holder = new DiscoverCommentMilestoneViewHolder(view);
                break;
            case LOAD_MORE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment_load_more, parent, false);
                holder = new DiscoverCommentLoadHolder(view);
                ((DiscoverCommentLoadHolder)holder).setOnClickLoadMoreListener(this);
                return holder;
        }
        if (holder != null) {
            ((DiscoverCommentViewHolder)holder).setOnClickDiscoverCommentListener(this);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(getItemViewType(position) == LOAD_MORE) {
            // 加载更多...
        } else {
            ((DiscoverCommentViewHolder) holder).setName(observer.userName(position));
            ((DiscoverCommentViewHolder) holder).setHeadUrl(observer.userUrl(position));
            ((DiscoverCommentViewHolder) holder).setComment(observer.comment(position));
            ((DiscoverCommentViewHolder) holder).setLike(observer.like(position));
            ((DiscoverCommentViewHolder) holder).setTime(observer.time(position));
            ((DiscoverCommentViewHolder) holder).setFrom(observer.from(position));
            switch (getItemViewType(position)) {
                case TEXT:
                    DiscoverCommentTextViewHolder textViewHolder = (DiscoverCommentTextViewHolder) holder;
                    textViewHolder.setDiaryText(observer.text(position));
                    break;
                case PHOTO:
                    // 显示第一张图片
                    DiscoverCommentPhotoViewHolder photoViewHolder = (DiscoverCommentPhotoViewHolder) holder;
                    photoViewHolder.setPhotoUrl(observer.imageUrl(position));
                    break;
                case AUDIO:
                    // 显示默认图片
                    break;
                case VIDEO:
                    // 显示视频缩略图及时长
                    DiscoverCommentVideoViewHolder videoViewHolder = (DiscoverCommentVideoViewHolder) holder;
                    videoViewHolder.setVideoImageUrl(observer.imageUrl(position));
                    videoViewHolder.setVideoDuration(observer.duration(position));
                    break;
                case MILESTONE:
                    // 显示具体内容
                    DiscoverCommentMilestoneViewHolder milestoneViewHolder = (DiscoverCommentMilestoneViewHolder) holder;
                    milestoneViewHolder.setMilestoneText(observer.text(position));
                    break;
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(!end) {
            if(observer.length() == position) {
                return LOAD_MORE;
            }
        }
        return observer.type(position);
    }

    @Override
    public int getItemCount() {
        if(end) {
            return observer.length();
        } else {
            return observer.length() + 1;
        }
    }

    @Override
    public void onClickLayout(DiscoverCommentViewHolder holder) {
        if(mCommentListener != null) {
            getCommentListener().onClickLayout(holder);
        }
    }

    @Override
    public void onClickHead(DiscoverCommentViewHolder holder) {
        if(mCommentListener != null) {
            getCommentListener().onClickHead(holder);
        }
    }

    public void setObserver(Observer observer) {
        this.observer = observer;
    }
}
